package com.example.testhiber.initPackage.business.controller;


import com.example.testhiber.initPackage.business.utils.FileUtils;
import com.example.testhiber.initPackage.business.utils.StringKit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.PostConstruct;
import java.io.File;
import java.util.ResourceBundle;
import java.util.UUID;

@Controller
public class PictureController {
    private static final Logger logger = LoggerFactory.getLogger(PictureController.class);


    /**
     * initPicture
     * @return
     */
    @ResponseBody
    @RequestMapping("/initPicture")
    public String initPicture(){

        ResourceBundle rb = ResourceBundle.getBundle("application");
        String fileInitPath = rb.getString("fileInitPath");//初始化文件目录
        String fileToPath = rb.getString("fileToPath");//分割到的文件目录
        String fileMaxToMove = rb.getString("fileMaxToMove");//设置多少个文件就移动到新的目录
        Integer fileMaxToMoveInt = Integer.parseInt(fileMaxToMove);

        File file = new File(fileInitPath);
        String filelist[] = file.list();
        Integer count = 1;

        String toPathUUID = "";
        for (String s:filelist){
            System.out.println(s);
            String fromPath=fileInitPath + s;//初始化文件路径

            if("".equals(StringKit.toString(toPathUUID))){
                toPathUUID = UUID.randomUUID().toString().replaceAll("-","");
                FileUtils.createFiles(fileToPath + toPathUUID);
            }

            String toPath= fileToPath + toPathUUID + File.separator + s;
            FileUtils.moveFile(fromPath,toPath);

            if(count > fileMaxToMoveInt){
                count=0;
                toPathUUID="";
            }
            count++;
        }
        System.out.println(count);

        return "操作成功";
    }


    /**
     * @Title:
     * @Description: TODO(项目启动会自动加载一下方法)
     * @param @param 
     * @return @return 
     * @author fcy
     * @throws
     * @date  
     */
    @PostConstruct
    public String initPicture2(){

        System.out.println("initPicture2 start");
//        ResourceBundle rb = ResourceBundle.getBundle("application");
//        String fileInitPath = rb.getString("fileInitPath");//初始化文件目录
//        String fileToPath = rb.getString("fileToPath");//分割到的文件目录
//        String fileMaxToMove = rb.getString("fileMaxToMove");//设置多少个文件就移动到新的目录
//        Integer fileMaxToMoveInt = Integer.parseInt(fileMaxToMove);
//
//        File file = new File(fileInitPath);
//        String filelist[] = file.list();
//        Integer count = 1;
//
//        String toPathUUID = "";
//        for (String s:filelist){
//            System.out.println(s);
//            String fromPath=fileInitPath + s;//初始化文件路径
//
//            if("".equals(StringKit.toString(toPathUUID))){
//                toPathUUID = UUID.randomUUID().toString().replaceAll("-","");
//                FileUtils.createFiles(fileToPath + toPathUUID);
//            }
//
//            String toPath= fileToPath + toPathUUID + File.separator + s;
//            FileUtils.moveFile(fromPath,toPath);
//
//            if(count>2){
//                count=0;
//                toPathUUID="";
//            }
//            count++;
//        }
//        System.out.println(count);

        return "操作成功";
    }

}